
// Source File Name:   CTBException.java
package com.sunwayhorizo.health.hl7.common;

import java.io.*;
import java.util.*;

// Referenced classes of package oracle.apps.ctb.fwk.base.common:
//            CTBAttributeException, CTBRowException

public class Hl7Exception extends Exception
{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String RCS_ID = "$Header: CTBException.java 1.0 2006/04/02 00:33:28 sulee noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: RIMException.java 1.0 2006/04/02 00:33:28 sulee noship $", "com.sunwayhorizo.health.hl7.common");
    public static final String TYPE_PARAM = "TYPE_PARAM";
    public static final String CODE_PARAM = "CODE_PARAM";
    public static final String MESSAGE_PARAM = "MESSAGE_PARAM";
    public static final String DETAIL_EXCEPTIONS_PARAM = "DETAIL_EXCEPTIONS_PARAM";
    public static final String BUNDLED_EXCEPTIONS_PARAM = "BUNDLED_EXCEPTIONS_PARAM";
    public static final String ERROR_TYPE = "ERROR";
    public static final String WARNING_TYPE = "WARNING";
    public static final String INFO_TYPE = "INFO";
    protected Vector m_Attrs;
    protected String m_OriginatingStackTrace;
    protected ServiceException m_service;
    protected Hl7Exception rimExceptions[];

    public void initalize()
    {
        m_Attrs = new Vector();
        m_Attrs.addElement("TYPE_PARAM");
        m_Attrs.addElement("CODE_PARAM");
        m_Attrs.addElement("MESSAGE_PARAM");
    }

    public Hl7Exception()
    {
        m_Attrs = new Vector();
        initalize();
    }

    public Hl7Exception(Exception exception)
    {
        m_Attrs = new Vector();
        initalize();
        m_service = new ServiceException(null, exception.getMessage(), null, null);
        StringWriter stringwriter = new StringWriter();
        PrintWriter printwriter = new PrintWriter(stringwriter);
        exception.printStackTrace(printwriter);
        setOriginatingStackTrace(stringwriter.toString());
    }

    public Hl7Exception(String s)
    {
        m_Attrs = new Vector();
        initalize();
        m_service = new ServiceException(null, s, null, null);
    }

    public Hl7Exception(String s, String s1, ServiceException aserviceexception[])
    {
        m_Attrs = new Vector();
        initalize();
        m_service = new ServiceException(s, s1, null, aserviceexception);
    }

    public Hl7Exception(ServiceException serviceexception)
    {
        m_Attrs = new Vector();
        initalize();
        m_service = serviceexception;
    }

    public Hl7Exception(String s, String s1, ServiceException aserviceexception[], ServiceException aserviceexception1[])
    {
        m_Attrs = new Vector();
        initalize();
        m_service = new ServiceException(s, s1, aserviceexception, aserviceexception1);
    }

    public String getOriginatingStackTrace()
    {
        return m_OriginatingStackTrace;
    }

    public void setOriginatingStackTrace(String s)
    {
        m_OriginatingStackTrace = s;
    }

    public void setType(String s)
    {
        m_service.setType(s);
    }

    public String getType()
    {
        return m_service.getType();
    }

    public String getMessage()
    {
        return m_service.getMessage();
    }

    public void setCode(String s)
    {
        setParameter("CODE_PARAM", s);
    }

    public String getCode()
    {
        return m_service.getCode();
    }

/*    public ServiceException[] getDetails()
    {
        return m_service.getDetailExceptions();
    }

    public Hl7Exception[] getBundledExceptions()
    {
        if(ctbExceptions != null && ctbExceptions.length > 0)
            return ctbExceptions;
        ServiceException aserviceexception[] = m_service.getBundledExceptions();
        if(aserviceexception == null)
            return new Hl7Exception[0];
        int i = aserviceexception.length;
        Hl7Exception actbexception[] = new Hl7Exception[i];
        for(int j = 0; j < i; j++)
            actbexception[j] = serviceExceptionToCTB(aserviceexception[j]);

        return actbexception;
    }
*/
    public void setBundledExceptions(ServiceException aserviceexception[])
    {
        setParameter("BUNDLED_EXCEPTIONS_PARAM", aserviceexception);
    }

    public void setBundledExceptions(Hl7Exception actbexception[])
    {
        rimExceptions = actbexception;
    }

/*    public Hl7Exception[] getDetailExceptions()
    {
        ServiceException aserviceexception[] = m_service.getDetailExceptions();
        if(aserviceexception == null)
            return new Hl7Exception[0];
        int i = aserviceexception.length;
        Hl7Exception actbexception[] = new Hl7Exception[i];
        for(int j = 0; j < i; j++)
            actbexception[j] = serviceExceptionToCTB(aserviceexception[j]);

        return actbexception;
    }
*/
    public Serializable getParameter(String s)
    {
        return m_service.getParameter(s);
    }

    public void setParameter(String s, Serializable serializable)
    {
        m_service.setParameter(s, serializable);
    }

    public static Hl7Exception serviceExceptionToCTB(ServiceException serviceexception)
    {
        Object obj = null;
        if(serviceexception instanceof ServiceAttributeException)
            obj = new AttributeException((ServiceAttributeException)serviceexception);
        else
        if(serviceexception instanceof ServiceRowException)
            obj = new RowException((ServiceRowException)serviceexception);
        else
        if(serviceexception instanceof ServiceException)
            obj = new Hl7Exception(serviceexception);
        return ((Hl7Exception) (obj));
    }

    public String getAttributesToString(Vector vector)
    {
        StringBuffer stringbuffer = new StringBuffer();
        String s;
        String s1;
        for(Enumeration enumeration = vector.elements(); enumeration.hasMoreElements(); stringbuffer.append(s + " = " + s1 + "\n"))
        {
            s = (String)enumeration.nextElement();
            Serializable serializable = getParameter(s);
            s1 = "null";
            if(serializable != null)
                s1 = serializable.toString();
        }

        return stringbuffer.toString();
    }

    public String getSubExceptionsToString(String s, String s1, Hl7Exception actbexception[])
    {
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(s + "\n");
        for(int i = 0; i < actbexception.length; i++)
        {
            stringbuffer.append(s1 + i + "\n");
            stringbuffer.append(actbexception[i].toString(s1 + "." + i));
        }

        return stringbuffer.toString();
    }

    public String toString()
    {
        return toString("exception ");
    }

    public String toString(String s)
    {
        StringBuffer stringbuffer = new StringBuffer();
        stringbuffer.append(getClass().getName() + "\n");
        stringbuffer.append(getAttributesToString(m_Attrs));
        stringbuffer.append("Originating Stacktrace:\n");
        stringbuffer.append(getOriginatingStackTrace());
//        stringbuffer.append(getSubExceptionsToString("Bundled Exceptions:", s, getBundledExceptions()));
//        stringbuffer.append(getSubExceptionsToString("Detail Exceptions:", s, getDetailExceptions()));
        return stringbuffer.toString();
    }

    public ServiceException getServiceException()
    {
        return m_service;
    }

}
